/**
 * MineAdmin is committed to providing solutions for quickly building web applications
 * Please view the LICENSE file that was distributed with this source code,
 * For the full copyright and license information.
 * Thank you very much for using MineAdmin.
 *
 * @Author X.Mo<root@imoi.cn>
 * @Link   https://github.com/mineadmin
 */
import type { MaProTableColumns, MaProTableExpose } from "@mineadmin/pro-table";
import type { UseDialogExpose } from "@/hooks/useDialog.ts";

import defaultAvatar from "@/assets/images/defaultAvatar.jpg";
import { ElTag } from "element-plus";
import { useMessage } from "@/hooks/useMessage.ts";
import { ResultCode } from "@/utils/ResultCode.ts";
import hasAuth from "@/utils/permission/hasAuth.ts";
import { deleteByIds } from "$/attractions/commerce/api/commerceQualification.ts";
import { page as merchantPage } from "$/attractions/commerce/api/commerceMerchant.ts";

export default function getTableColumns(
  dialog: UseDialogExpose,
  formRef: any,
  t: any
): MaProTableColumns[] {
  const msg = useMessage();

  const merchantList = ref<any[]>([]);

  onMounted(async () => {
    const res2 = await merchantPage({
      page: 1,
      pageSize: 1000,
    });
    if (res2.code === 200 && res2.data && res2.data.list) {
      merchantList.value = res2.data.list;
    }
  });

  return [
    {
      type: "selection",
      showOverflowTooltip: false,
      label: () => t("crud.selection"),
    },
    {
      label: "商户名称",
      prop: "merchant_id",
      cellRender: ({ row }) => {
        const merchantName = merchantList.value.find(
          (merchant) => merchant.id === row.merchant_id
        );
        return merchantName?.merchant_name || `商户名称${row.merchant_id}`;
      },
    },
    {
      label: "资质类型",
      prop: "qualification_type",
      cellRender: ({ row }) => {
        const statusMap = {
          1: { text: "食品经营许可证", type: "success" },
          2: { text: "卫生许可证", type: "success" },
          3: { text: "消防许可证", type: "warning" },
          4: { text: "其他证书", type: "info" },
        };
        const status = statusMap[row.qualification_type] || {
          text: "未知",
          type: "info",
        };
        return h(ElTag, { type: status.type }, () => status.text);
      },
    },
    { label: "资质名称", prop: "qualification_name" },
    {
      label: "资质证书号",
      prop: "qualification_id",
      renderProps: { multiple: false },
    },
    { label: "发证机关", prop: "issuing_authority" },
    { label: "发证日期", prop: "issue_date", mode: "date", showTime: false },
    {
      label: "资质文件",
      prop: "qualification_file",
      multiple: false,
      cellRender: ({ row }) => {
        if (!row?.qualification_file) {
          return <span style={{ color: '#999' }}>无文件</span>;
        }
        
        // 获取API基础URL
        const env = import.meta.env;
        const baseUrl = env.VITE_OPEN_PROXY === 'true' ? env.VITE_PROXY_PREFIX : env.VITE_APP_API_BASEURL;
        const fileUrl = `${baseUrl}${row.qualification_file}`;
        
        return (
          <el-link
            onClick={() => {
              // 创建下载链接
              const link = document.createElement('a');
              link.href = fileUrl;
              link.download = row.qualification_file.split('/').pop() || '资质文件';
              document.body.appendChild(link);
              link.click();
              document.body.removeChild(link);
            }}
            type="primary"
          >
            下载文件
          </el-link>
        );
      },
    },
    {
      type: "operation",
      label: () => t("crud.operation"),
      operationConfigure: {
        actions: [
          {
            name: "edit",
            icon: "material-symbols:person-edit",
            show: ({ row }) =>
              hasAuth("commerce_qualification:commerceQualify:update"),
            text: () => t("crud.edit"),
            onClick: ({ row }) => {
              dialog.setTitle(t("crud.edit"));
              dialog.open({ formType: "edit", data: row });
            },
          },
          {
            name: "del",
            icon: "mdi:delete",
            text: () => t("crud.delete"),
            show: ({ row }) =>
              hasAuth("commerce_qualification:commerceQualify:delete"),
            onClick: ({ row }, proxy: MaProTableExpose) => {
              msg.delConfirm(t("crud.delDataMessage")).then(async () => {
                const response = await deleteByIds([row.id]);
                if (response.code === ResultCode.SUCCESS) {
                  msg.success(t("crud.delSuccess"));
                  proxy.refresh();
                }
              });
            },
          },
        ],
      },
    },
  ];
}
